package list;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        //测试新建英雄节点
        HeroNode2 n1 = new HeroNode2(1,"宋江", "及时雨");
        HeroNode2 n2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 n3 = new HeroNode2(6, "吴用", "智多星");
        HeroNode2 n4 = new HeroNode2(4, "林冲", "豹子头");
        DoubleLinkedList l1 = new DoubleLinkedList();
        //测试尾插英雄节点
        l1.addHero(n1);
        l1.addHero(n2);
        //l1.addHero(n3);
        l1.addHero(n4);
        //测试显示英雄信息
        l1.list();
        System.out.println("----------------------------------");
        //测试插入英雄节点
        l1.insertHero(n3);
        //测试根据num修改英雄信息
        //l1.update(n3);
        //测试删除英雄节点
        //l1.deleteHero(1);
        l1.deleteHero(4);
        l1.list();
    }
}


//定义DoubleLinkedList 管理英雄
class DoubleLinkedList {
    private HeroNode2 head = new HeroNode2(0, "", "");

    //①添加英雄节点
    public void addHero(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        while (true) {
            if(temp.next == null)
                break;
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }
    //②插入英雄
    public void insertHero(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        if(heroNode.num < 1 )
            return;
        while(temp != null) {
            if(temp.next != null && temp.next.num < heroNode.num)
                temp = temp.next;
            else if (temp.next != null && temp.next.num == heroNode.num) {
                System.out.println("number"+heroNode.num+":repeat!");
                break;
            } else{
                heroNode.next = temp.next;
                temp.next = heroNode;
                heroNode.pre = temp;
                if(heroNode.next != null)
                    heroNode.next.pre = heroNode;
                break;
            }
        }
    }
    //③根据num修改英雄属性
    public void update(HeroNode2 heroNode) {
        HeroNode2 temp = head.next;
        while(temp != null){
            if(heroNode.num == temp.num) {
                temp.name = heroNode.name;
                temp.nickname = heroNode.nickname;
                return;
            }
            else {
                temp = temp.next;
            }
        }
    }
    //④显示链表
    public void list() {
        if(head.next == null) {
            System.out.println("链表为空!");
            return;
        }
        HeroNode2 temp = head.next;
        while(true) {
            if(temp == null)
                break;
            System.out.println(temp);
            temp = temp.next;
        }


    }
    //⑤删除节点
    public void deleteHero(int num) {
        if(head.next == null)
            return;
        HeroNode2 temp = head.next;
        boolean flag = false;
        while(temp != null) {
            if(temp.num == num) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            if(temp.next != null)
            temp.next.pre = temp.pre;
            temp.pre.next = temp.next;
        }
        else {
            System.out.printf("没有找到待删除节点%d\n",num);
        }
    }
}



class HeroNode2{
    public int num;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;
    //构造器
    public HeroNode2(int num, String name, String nickname) {
        this.num = num;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                ", "+ '}';
    }
}